package com.cloudcc.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.chat.DraftBox;
import com.cloudcc.mobile.util.Tools;
/**
 * 草稿箱适配器
 * @author Administrator
 *
 */
public class DraftBoxAdapter extends BaseAdapter{
	private List<DraftBox> list;
	private Context context;
	private OnSendListener onSendListener;
	public DraftBoxAdapter(List<DraftBox> list,Context context){
		this.list=list;
		this.context=context;
	}
	public void changData(List<DraftBox> list){
		this.list=list;
		notifyDataSetChanged();
	}

	public interface OnSendListener{
		public void send(int position,View v);
		public void listviewbtn(int position);
		public void LongClicked(ContextMenu menu,int position);
	}
	public void setOnSendListener(OnSendListener onSendListener){
		this.onSendListener=onSendListener;
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public DraftBox getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if(convertView==null){
			convertView=LayoutInflater.from(context).inflate(R.layout.draftbox_list_item, null);
			holder=new ViewHolder();
			holder.name=(TextView)convertView.findViewById(R.id.name);
			holder.text=(TextView)convertView.findViewById(R.id.text);
			holder.time=(TextView)convertView.findViewById(R.id.time);
			holder.num=(TextView)convertView.findViewById(R.id.num_caogao);
			holder.send=(Button)convertView.findViewById(R.id.send_caogao);
			holder.listitem=(LinearLayout)convertView.findViewById(R.id.listviewbtn);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder) convertView.getTag();
		}
		final DraftBox box=list.get(position);
		String text="";
		if(box.getType().equals("1")){
			holder.name.setText("签到");
			text=box.getKhbf().getBfText();
		}else if(box.getType().equals("2")){
			holder.name.setText("微贴");
			Tools.i("leixing", box.getLeixing());
			if(box.getLeixing().equals("addMicroPostF")){
				text=box.getAddMicroPostF().getBody();
			}else if(box.getLeixing().equals("addMicroPostDMany")){
				text=box.getAddMicroPostDMany().getBody();
			}else if(box.getLeixing().equals("addMicroPostL")){
				text=box.getAddMicroPostL().getBody();
			}else if(box.getLeixing().equals("addMicroPostV")){
				text=box.getAddMicroPostV().getBody();
			}
		}else if(box.getType().equals("3")){
			holder.name.setText("回复");
			text=box.getComment().getBody();
		}
		holder.time.setText(box.getTime());
		holder.num.setText(""+(position+1));
		if(!Tools.isNull(text)){
			if(text.length()>20){
				String neirong=text.substring(0, 19);
				holder.text.setText(neirong);
			}else{
				holder.text.setText(text);
			}
		}
		holder.send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean flag=box.isClickable();
				if(box.isClickable()){
					onSendListener.send(position,v);
				}else{
				}
			}
		});
		holder.listitem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onSendListener.listviewbtn(position);
			}
		});
		holder.listitem.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				onSendListener.LongClicked(menu, position);
			}
		});
		return convertView;
	}
	class ViewHolder{
		TextView name;
		TextView time;
		TextView text;
		TextView num;
		Button send;
		LinearLayout listitem;
	}
}
